package com.heba.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heba.bean.diary.ItemDiaryImageObject;
import com.heba.bean.diary.ItemPasteObject;
import com.heba.config.CommonConfig;
import com.heba.greenmulu.R;
import com.heba.greenmulu.diary.ItemPersonHomePageActivity;
import com.heba.utils.GlideUtil;
import com.heba.utils.ViewUtils;

import java.util.List;

/**
 *
 * @author jason
 */
public class DiaryImageShowLayout extends FrameLayout {

    private FrameLayout layouPoints;

    private ImageView imgBg;

    private Context mContext;

    public DiaryImageShowLayout(Context context) {
        this(context, null);
    }

    public DiaryImageShowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DiaryImageShowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context, attrs);
    }

    /**
     * initView
     *
     * @param context
     * @param attrs
     */
    private void initView(Context context, AttributeSet attrs) {

        mContext = context;

        View imgPointLayout = inflate(context, R.layout.layout_imgview_tag, this);

        imgBg = imgPointLayout.findViewById(R.id.imgBg);
        layouPoints = imgPointLayout.findViewById(R.id.layouPoints);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    /**
     *
     * @param width
     * @param height
     * @param object
     */
    public void setImgBg(int width, int height, ItemDiaryImageObject object) {
        ViewGroup.LayoutParams lp = imgBg.getLayoutParams();
        lp.width = width;
        lp.height = height;

        imgBg.setLayoutParams(lp);

        ViewGroup.LayoutParams lp1 = layouPoints.getLayoutParams();
        lp1.width = width;
        lp1.height = height;

        layouPoints.setLayoutParams(lp1);

        GlideUtil.loadImageWithDefaultPlaceHolder(mContext, object.getImageUrl(), imgBg);

        /**
         */
        List<ItemPasteObject> list = object.getPaste();
        for (int i = 0; i < list.size(); i++) {
            ItemPasteObject pasteObject = list.get(i);
            if (!pasteObject.getUserID().isEmpty())
                addItemToScalePosition(width, height, pasteObject);
        }

    }

    /**
     *
     * @param width
     * @param height
     * @param object
     */
    private void addItemToScalePosition(int width, int height, ItemPasteObject object) {
        float width_scale = 0.5f, height_scale = 0.5f;
        try {
            if (object.getPos_x() != null && !object.getPos_x().isEmpty())
                width_scale = Float.valueOf(object.getPos_x());
            if (object.getPos_y() != null && !object.getPos_y().isEmpty())
                height_scale = Float.valueOf(object.getPos_y());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (width_scale > 1f) width_scale = 0.5f;
        if (height_scale > 1f) height_scale = 0.5f;

        LinearLayout view = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_img_tag, this, false);
        TextView tv_tag_view = view.findViewById(R.id.tv_tag_view);
        ImageView iv_delete = view.findViewById(R.id.iv_delete);
        tv_tag_view.setText("@" + object.getAccount());
        iv_delete.setVisibility(View.INVISIBLE);

        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();

        int imgWidth = ViewUtils.getViewHeight(view, false);
        int imgHeight = ViewUtils.getViewHeight(view, true);

        layoutParams.leftMargin = (int) (width * width_scale) - imgWidth / 2;
        layoutParams.topMargin = (int) (height * height_scale) - imgHeight / 2;

        iv_delete.setOnClickListener(v -> layouPoints.removeAllViews());

        tv_tag_view.setOnClickListener(v -> {
            Intent intent = new Intent(mContext, ItemPersonHomePageActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString(CommonConfig.SEND_USER_ID, object.getUserID());
            intent.putExtras(bundle);
            mContext.startActivity(intent);
            ((Activity) mContext).overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

        });

        layouPoints.addView(view, layoutParams);
    }

    /**
     * 添加tag
     *
     * @param x
     * @param y
     */
    private void addItemPosition(int x, int y, String name) {
        layouPoints.removeAllViews();

        LinearLayout view = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_img_tag, this, false);
        TextView tv_tag_view = view.findViewById(R.id.tv_tag_view);
        tv_tag_view.setText(name);

        LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();

        int imgWidth = ViewUtils.getViewHeight(tv_tag_view, false);
        int imgHeight = ViewUtils.getViewHeight(tv_tag_view, true);

        layoutParams.leftMargin = x - imgWidth / 2;
        layoutParams.topMargin = y - imgHeight / 2;

        /**
         */
        if (layoutParams.topMargin < 0) {
            layoutParams.topMargin = 0;
        } else if (layoutParams.topMargin > getHeight()) {
            layoutParams.topMargin = getHeight() - imgHeight;
        }

        layouPoints.addView(view, layoutParams);
    }
}
